# Article Title: Female Representation in Legislative Committees and Perceptions of Legitimacy: Evidence from a Harmonized Experiment in Jordan, Morocco, and Tunisia
# Authors: Kritsen Kao, Ellen Lust, Marwa Shalaby, and Chagai Weiss
# Purpose: Code for Figure 2-5 in the main text


#Load Relevant packages------
library("tidyverse")
library("estimatr")
library("ggplot2")
library("effectsize")
library("ggthemes")
library("texreg")
library("ltm")
library("stargazer")
library("RItools")
library("xtable")
library("MASS")
library("modelsummary")

#Load data for analyses
#Read in data----
gend_dat <- readRDS("replication_data/data_for_analysis.rds")
# Subset to experiments where issue area is domestic violence. These are the
# experiments used in the main analyses
gend_domv <-
 gend_dat %>% 
 filter(.,
        d_issue_dv == 1)



#Figure 2------
## Estimate regressions------
h1_ix <-
 gend_domv %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal +d_pro_dec+
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           se_type = "HC0",
           data = .) 
h1_ix_p <- 
 tidy(h1_ix) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Pooled",
        Outcome = "Right \nDecision \nIndex")

h1_ix_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal + d_pro_dec+
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h1_ix_jrd_p <- 
 tidy(h1_ix_jrd) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Jordan",
        Outcome = "Right \nDecision \nIndex")


h1_ix_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h1_ix_tns_p<- 
 tidy(h1_ix_tns) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Right \nDecision \nIndex")



h1_ix_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal + d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h1_ix_mrc_p <-
 tidy(h1_ix_mrc) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Morocco",
        Outcome = "Right \nDecision \nIndex")


## Plot regressions----- 
# Combine all models
h1_coef <- rbind(h1_ix_p, h1_ix_jrd_p, h1_ix_tns_p, h1_ix_mrc_p) %>% 
 mutate(.,
        term = ifelse(term == "d_gen_bal", 
                      "Gender \nBalance",
                      "Pro Women \nDecision"))


# Set order of outcomes and countries
h1_coef$Sample <- factor(h1_coef$Sample, 
                         c("Pooled",
                           "Jordan",
                           "Tunisia",
                           "Morocco"
                         ))

positions_h1 <- c("Gender \nBalance",
                  "Pro Women \nDecision")

ggplot(h1_coef, aes(y=estimate, x=term, 
                    color = Sample, shape = Sample)) +
 geom_hline(yintercept = 0, color = "gray50", linetype = 2, linewidth = 0.2) +
 geom_pointrange(aes(ymin = conf.low, ymax = conf.high), 
                 position = position_dodge(width = 0.4)) +
 scale_color_brewer(palette = "PuOr")+
 scale_x_discrete(limits = positions_h1)+ 
 labs(x = "",
      y = "ATE on Right Decision Index",
      color = "",
      shape = "") +
 theme_tufte()+
 theme(text = element_text(size = 12, family = "Times"),
       legend.position = "bottom",
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank())



#Figure 3-----
## Estimate regressions------
h2_ix <-
 gend_domv %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           se_type = "HC0",
           data = .)
h2_ix_p <-
 tidy(h2_ix) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Pooled",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")




h2_ix_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h2_ix_jrd_p<-
 tidy(h2_ix_jrd) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Jordan",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")


h2_ix_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h2_ix_tns_p <- 
 tidy(h2_ix_tns) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")



h2_ix_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal + d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .)
h2_ix_mrc_p <- 
 tidy(h2_ix_mrc) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Morocco",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")


##Plot regressions-----
# Combine all models
h2_coef <- rbind(h2_ix_p, h2_ix_jrd_p, h2_ix_tns_p, h2_ix_mrc_p) %>% 
 mutate(.,
        term = ifelse(term == "d_gen_bal", 
                      "Gender \nBalance", 
                      "Pro Women \nDecision"))


# Set order of outcomes and countries
h2_coef$Sample <- factor(h2_coef$Sample, 
                         c("Pooled",
                           "Jordan",
                           "Tunisia",
                           "Morocco"
                         ))

positions_h2 <- c("Gender \nBalance",
                  "Pro Women \nDecision")

ggplot(h2_coef, aes(y=estimate, x=term, 
                    color = Sample, shape = Sample)) +
 geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
 geom_pointrange(aes(ymin = conf.low, ymax = conf.high), 
                 position = position_dodge(width = 0.4)) +
 scale_color_brewer(palette = "PuOr")+
 scale_x_discrete(limits = positions_h2)+ 
 labs(x = "",
      y = "ATE on Attitudes towards Comittee",
      color = "",
      shape = "") +
 theme_tufte()+
 theme(text = element_text(size = 12, family = "Times"),
       legend.background=element_blank(),
       legend.position = "bottom",
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank())

#Figure 4-----
##Estimate regressions-----
h3 <-
 gend_domv %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           se_type = "HC0",
           data = .)
h3_p <-
 tidy(h3) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Pooled",
        Outcome = "Public \nAccept \nDecision")




h3_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h3_jrd_p <-
 tidy(h3_jrd) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Jordan",
        Outcome = "Public \nAccept \nDecision")


h3_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal + d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h3_tns_p <-
 tidy(h3_tns) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>%  
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Public \nAccept \nDecision")



h3_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal + d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h3_mrc_p <- 
 tidy(h3_mrc) %>%
 #mutate_if(is.numeric, round, 3) %>% 
 filter(term %in% c("d_gen_bal", "d_pro_dec"))%>% 
 mutate(.,
        Sample = "Morocco",
        Outcome = "Public \nAccept \nDecision")


##Plot regressions------
# Combine all models
h3_coef <- rbind(h3_p, h3_jrd_p, h3_tns_p, h3_mrc_p) %>% 
 mutate(.,
        term = ifelse(term == "d_gen_bal", "Gender \nBalance",
                      "Pro Women \nDecision"))


# Set order of outcomes and countries
h3_coef$Sample <- factor(h3_coef$Sample, 
                         c("Pooled",
                           "Jordan",
                           "Tunisia",
                           "Morocco"
                         ))

ggplot(h3_coef, aes(y=estimate, x=term, 
                    color = Sample, shape = Sample)) +
 geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
 geom_pointrange(aes(ymin = conf.low, ymax = conf.high), 
                 position = position_dodge(width = 0.4)) +
 scale_color_brewer(palette = "PuOr")+
 labs(x = "",
      y = "ATE on Blief of Public Acceptance",
      color = "",
      shape = "") +
 theme_tufte()+
 theme(text = element_text(size = 12, family = "Times"),
       legend.background=element_blank(),
       legend.position = "bottom",
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank())


#Figure 5------
## Estimate regressions------
###Outcome: Committee Made Right decision (Index)-----
h4_rt_dec_pool <-
 gend_domv %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal*d_pro_dec+
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           data = .) 
h4_rt_dec_pool_p <- 
 tidy(h4_rt_dec_pool) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Pooled",
        Outcome = "Right \nDecision \nIndex")


h4_rt_dec_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal*d_pro_dec+
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h4_rt_dec_jrd_p <-
 tidy(h4_rt_dec_jrd) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Jordan",
        Outcome = "Right \nDecision \nIndex")


h4_rt_dec_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h4_rt_dec_tns_p <- 
 tidy(h4_rt_dec_tns) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>%
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Right \nDecision \nIndex")



h4_rt_dec_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_right_dec_ix = standardize(y_right_dec_ix)) %>% 
 lm_robust(y_right_dec_ix ~
            d_gen_bal*d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h4_rt_dec_mrc_p<-
 tidy(h4_rt_dec_mrc) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>%
 mutate(.,
        Sample = "Morocco",
        Outcome = "Right \nDecision \nIndex")

###Outcome: Attitudes Towards Committee (Index)-----
h4_att_cmt_pool <-
 gend_domv %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           se_type = "HC0",
           data = .) 
h4_att_cmt_pool_p <-
 tidy(h4_att_cmt_pool) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>%
 mutate(.,
        Sample = "Pooled",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")



h4_att_cmt_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h4_att_cmt_jrd_p <-
 tidy(h4_att_cmt_jrd) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>%
 mutate(.,
        Sample = "Jordan",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")


h4_att_cmt_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .)
h4_att_cmt_tns_p <-
 tidy(h4_att_cmt_tns) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")



h4_att_cmt_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_att_comittee = standardize(y_att_comittee)) %>% 
 lm_robust(y_att_comittee ~
            d_gen_bal*d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 

h4_att_cmt_mrc_p <-
 tidy(h4_att_cmt_mrc) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Morocco",
        Outcome = "Attitude \nTowards \nCommittee \nIndex")


### Outcome: Acceptance of Committee Decision------

h4_accpt_cmt_pool <-
 gend_domv %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           fixed_effects = x_cntry,
           se_type = "HC0",
           data = .) 
h4_accpt_cmt_pool_p <-
 tidy(h4_accpt_cmt_pool) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Pooled",
        Outcome = "Public \nAccept \nDecision")




h4_accpt_cmt_jrd <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Jordan") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .)
h4_accpt_cmt_jrd_p <- 
 tidy(h4_accpt_cmt_jrd) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Jordan",
        Outcome = "Public \nAccept \nDecision")


h4_accpt_cmt_tns <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Tunisia") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal*d_pro_dec +
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h4_accpt_cmt_tns_p <-
 tidy(h4_accpt_cmt_tns) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>% 
 mutate(.,
        Sample = "Tunisia",
        Outcome = "Public \nAccept \nDecision")



h4_accpt_cmt_mrc <-
 gend_domv %>% 
 filter(.,
        x_cntry == "Morocco") %>% 
 mutate(.,
        y_public_accept = standardize(y_public_accept)) %>% 
 lm_robust(y_public_accept ~
            d_gen_bal*d_pro_dec +  
            x_age + x_edu +x_male,
           se_type = "HC0",
           data = .) 
h4_accpt_cmt_mrc_p <-
 tidy(h4_accpt_cmt_mrc) %>%
 filter(term %in% c("d_gen_bal", "d_pro_dec",
                    "d_gen_bal:d_pro_dec")) %>%  
 mutate(.,
        Sample = "Morocco",
        Outcome = "Public \nAccept \nDecision")

##Plot regressions------
h4_coef <- 
 rbind(h4_rt_dec_pool_p, h4_rt_dec_jrd_p, h4_rt_dec_tns_p, h4_rt_dec_mrc_p,
       h4_att_cmt_pool_p, h4_att_cmt_jrd_p, h4_att_cmt_tns_p, h4_att_cmt_mrc_p,
       h4_accpt_cmt_pool_p, h4_accpt_cmt_jrd_p, h4_accpt_cmt_tns_p, h4_accpt_cmt_mrc_p
 ) %>% 
 mutate(.,
        term = case_when(
         term == "d_gen_bal" ~ "Gender \nBalance",
         term == "d_pro_dec" ~ "Pro \nWomen",
         term == "d_gen_bal:d_pro_dec" ~ "Balance*\nPro",
        ))


# Set order of outcomes and countries
h4_coef$Sample <- factor(h4_coef$Sample, 
                         c("Pooled",
                           "Jordan",
                           "Tunisia",
                           "Morocco"
                         ))

h4_coef$Outcome <- factor(h4_coef$Outcome, 
                          c("Right \nDecision \nIndex",
                            "Attitude \nTowards \nCommittee \nIndex",
                            "Public \nAccept \nDecision"
                          ))

h4_pos <- c("Pooled",
            "Jordan",
            "Tunisia",
            "Morocco")

ggplot(h4_coef, aes(x=estimate, y=term, 
                    color = Sample, shape = Sample)) +
 geom_vline(xintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
 geom_pointrange(aes(xmin = conf.low, xmax = conf.high), 
                 position = position_dodge(width = 0.4)) +
 facet_wrap(~ Outcome)+
 labs(x = "Effects Size",
      y = "",
      color = "",
      shape = "") +
 scale_color_brewer(palette = "PuOr")+
 theme(text = element_text(size = 12, family = "Times"),
       legend.position = "bottom",
       legend.key=element_blank(),
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 10, family = "Times",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(), 
       axis.line = element_line(colour = "black"))
